package com.hkpickup.web;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

/**
 * Display information of client's request
 * 
 * reference: 
 * <a href="http://code.google.com/intl/zh-TW/appengine/docs/java/urlfetch/usingjavanet.html">google sample</a>
 */
@SuppressWarnings("serial")
public class SimpleRequestServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		try {
	        URL url = new URL("http://horsedb.dyndns.org");
	        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
	        String line = null;
	        StringBuffer webContent = new StringBuffer(); 
	        
	        int octet=0;
	        while ((octet=reader.read())!=-1) {	        
				if (Character.isISOControl((char)octet)) {
					webContent.append(' ');
				} else {
					webContent.append((char)octet);
				}
	        	//webContent.append(line);
	        }
	        reader.close();
	        
			resp.setContentType("text/html");
			resp.getWriter().println(webContent);
	        
	        
	    } catch (MalformedURLException e) {
	        // ...
	    	resp.getOutputStream().println(e.toString());
	    } catch (IOException e) {
	        // ...
	    	resp.getOutputStream().println(e.toString());
	    }
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		doGet(req,resp);
	}
}
